package ezy.widget.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

import ezy.widget.R;


public class IconTextButton extends TextView {

    private Paint mIconPaint;
    private Drawable mIconDrawable;
    private String mIconText;
    private String mIconFontPath;
    private ColorStateList mIconTextColor;
    private float mIconTextSize;
    private float mIconSize;
    private int mPaddingTop;

    public IconTextButton(Context context) {
        super(context);
        init(context, null);
    }

    public IconTextButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public IconTextButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconTextButton);
        mIconTextSize = getTextSize();
        float iconPadding = 0;
        String iconFontPath = null;
        try {
            iconPadding = a.getDimension(R.styleable.IconTextButton_iconPadding, 0);
            mIconText = a.getString(R.styleable.IconTextButton_iconText);
            mIconTextColor = a.getColorStateList(R.styleable.IconTextButton_iconTextColor);
            mIconTextSize = a.getDimension(R.styleable.IconTextButton_iconTextSize, mIconTextSize);
            mIconFontPath = a.getString(R.styleable.IconTextButton_iconFontPath);
            mIconDrawable = a.getDrawable(R.styleable.IconTextButton_iconDrawable);
            mIconSize = a.getDimension(R.styleable.IconTextButton_iconSize, mIconTextSize);
        } finally {
            a.recycle();
        }

        mIconPaint = PaintUtil.createTextPaint(getContext(), mIconTextSize, -1, -1, mIconFontPath);

        setPaintColor(mIconPaint, mIconTextColor);

        mPaddingTop = getPaddingTop();
        setPadding(getPaddingLeft(), getPaddingTop() + (int)(mIconSize + iconPadding), getPaddingRight(), getPaddingBottom());

    }

    private void setPaintColor(Paint paint, ColorStateList colors) {
        if (paint == null) {
            return;
        }
        if (colors != null) {
            paint.setColor(colors.getColorForState(getDrawableState(), 0));
        } else {
            paint.setColor(getCurrentTextColor());
        }
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        setPaintColor(mIconPaint, mIconTextColor);
        if (mIconDrawable != null) {
            mIconDrawable.setState(getDrawableState());
        }
    }

    int mTextX = 0;
    int mTextY = 0;
    int mIconX = 0;
    @Override
    protected void onDraw(Canvas canvas) {
        if (mIconX == 0) {
            Paint.FontMetrics metrics = mIconPaint.getFontMetrics();
            mTextX = (int)(getMeasuredWidth() - mIconTextSize) / 2;
            mTextY = mPaddingTop + (int)(mIconSize - metrics.bottom - metrics.top) / 2;

            mIconX = (int)(getMeasuredWidth() - mIconSize) / 2;
        }
        if (mIconDrawable != null) {
            mIconDrawable.setBounds(mIconX, mPaddingTop, mIconX + (int) mIconSize, mPaddingTop + (int) mIconSize);
            mIconDrawable.draw(canvas);
        }
        if (mIconText != null) {
            canvas.drawText(mIconText, getScrollX() + mTextX, mTextY, mIconPaint);
        }
        super.onDraw(canvas);
    }
    public void setIconText(String value) {
        mIconText = value;
        invalidate();
    }
    public void setmIconDrawable(Drawable value){
        mIconDrawable=value;
        invalidate();
    }
    public void setmIconSize(float IconSize){
        mIconSize=IconSize;
        invalidate();
    }
}